using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class CheckOutBook : System.Web.UI.UserControl
{
    //DateTime todayDate = DateTime.Today;

    protected void Page_Load(object sender, EventArgs e)
    {
        //lblDate.Text = DateTime.Today.ToString();
        CrystalReportViewer1.ReportSource = "Reports/CR_LibrarianCheckedOutBooksReceipt.rpt";
    }
    
    protected void txtPatronId_txtChange(object sender, EventArgs e)
    {
        //string pId = txtPatronId.Text;

    }
    protected void btnSearchPatron_Click(object sender, EventArgs e)
    {
        //DetailsView_Patron.DataBind();
        DataList_Patron.DataBind();
        //if (SqlDataSource_patron)
        //{
        //    lblErrorMsg.Text = "That Patron does not exist";
        //}
        //else
        //{
        //    lblErrorMsg.Text = "";
        //}
    }

    //-- Confirmation lable for searching for a person
    protected void SqlDataSource_patron_Selected(object sender, SqlDataSourceStatusEventArgs e)
    {
        if (e.AffectedRows < 1)
            lblErrorMsg.Text = "That Patron does not exist";
        else
            lblErrorMsg.Text = "";
    }

    protected void btnBarcode_Click(object sender, EventArgs e)
    {
        ManageBook b = new ManageBook();
        SqlDataAdapter sqlDat;
        Boolean exist;

        ManageCheckedOutBook cob = new ManageCheckedOutBook();
        Boolean cobExist;

        try
        {
            exist = b.bookExists(txtBarcode.Text);
            cobExist = cob.checkedOutBookExists(txtBarcode.Text);
            if (exist == false)
                lblErrorMsg.Text = "That Book does not exist";

            if (cobExist == true)
                lblErrorMsg.Text = "That book is currently checked out";

            if (exist == true & cobExist == false)
            {
                lblErrorMsg.Text = "";

                ManageCheckedOutBook mcoBook = new ManageCheckedOutBook();
                CheckedOutBook coBook = new CheckedOutBook();
                ManageLibrary l = new ManageLibrary();
                int length = l.getCheckoutLength(this.Request.QueryString.Get("id"));

                coBook.Barcode = txtBarcode.Text;
                coBook.dateCheckedOut = DateTime.Today;
                coBook.dueDate = DateTime.Today.AddDays(length);
                coBook.PersonID = int.Parse(txtPatronId.Text);
                coBook.status = "CheckedOut";

                mcoBook.checkoutBook(coBook);

                sqlDat = mcoBook.getBooksCheckedOut_LibrarianView(txtPatronId.Text);
                DataSet d = new DataSet();
                sqlDat.Fill(d);

                //GridView_CheckedOutBooks.DataSource = d;
                GridView_CheckedOutBooks.DataBind();
            }
        }
        catch (Exception ex)
        {
            lblErrorMsg.Text = ex.Message.ToString();
        }
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        txtPatronId.Text = "";
        txtBarcode.Text = "";
        lblErrorMsg.Text = "";
        pnlReport.Visible = false;
    }
    protected void btnReport_Click(object sender, EventArgs e)
    {
        pnlReport.Visible = true;
    }
}
